#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
简化的OCR安装脚本
"""

import subprocess
import sys
import time

def install_with_retry(package, max_retries=3):
    """带重试的安装"""
    for attempt in range(max_retries):
        print(f"尝试安装 {package} (第{attempt + 1}次)...")
        try:
            subprocess.check_call([
                sys.executable, "-m", "pip", "install", 
                package, 
                "-i", "https://pypi.tuna.tsinghua.edu.cn/simple/",
                "--trusted-host", "pypi.tuna.tsinghua.edu.cn",
                "--timeout", "300"  # 5分钟超时
            ])
            print(f"✅ {package} 安装成功!")
            return True
        except subprocess.CalledProcessError as e:
            print(f"❌ 第{attempt + 1}次安装失败: {e}")
            if attempt < max_retries - 1:
                print("等待5秒后重试...")
                time.sleep(5)
            else:
                print(f"❌ {package} 安装最终失败")
                return False

def main():
    print("=" * 50)
    print("    PaddleOCR 简化安装工具")
    print("=" * 50)
    print()
    
    # 先安装基础依赖
    print("正在安装基础依赖...")
    basic_packages = [
        "numpy",
        "opencv-python",
        "pillow"
    ]
    
    for package in basic_packages:
        install_with_retry(package)
    
    print("\n正在安装PaddleOCR...")
    print("这可能需要几分钟时间，请耐心等待...")
    
    # 安装PaddleOCR
    if install_with_retry("paddleocr"):
        print("\n🎉 PaddleOCR安装完成！")
        print("现在可以运行相机程序进行数字识别了。")
    else:
        print("\n⚠️ PaddleOCR安装失败，但程序仍可以运行（OCR功能不可用）。")
        print("您可以稍后手动安装：pip install paddleocr")
    
    input("\n按回车键退出...")

if __name__ == "__main__":
    main() 